#include "hallwidget.h"
#include "ui_hallwidget.h"
#include "clienthandler.h"
#include "userinfo.h"
#include "../common/singleton.h"

hallWidget::hallWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::hallWidget)
{
    ui->setupUi(this);

    ui->labUsername->setText(SingleTon<UserInfo>::getReference().getUsername());
    ui->listWidget->setCurrentRow(0);
}

hallWidget::~hallWidget()
{
    delete ui;
    roomMap.clear();
}

void hallWidget::addroom(QString username, QString roomid)
{
    roomMap[username]=roomid;
}

void hallWidget::getrooms()
{
    ClientHandlerGetRoom::transfer();
}

void hallWidget::showrooms()
{
    //显示房间号
    QMap<QString, QString>::Iterator it;
    for(it = roomMap.begin();
        it != roomMap.end();
        ++it)
        ui->listWidget->addItem(it.value());
}

void hallWidget::clearrooms()
{
    roomMap.clear();
    ui->listWidget->clear();
}


void hallWidget::on_btnCreateroom_clicked()
{
    QString roomid = ui->lineEdit->text();
    ClientHandlerCreateRoom::transfer(roomid);
}

void hallWidget::on_btngetroom_clicked()
{
    getrooms();
}

void hallWidget::on_btnJoinroom_clicked()
{
    if(ui->listWidget->currentItem()==nullptr){return;}
    QString roomid = ui->listWidget->currentItem()->text();
    ClientHandlerJoinRoom::transfer(roomid);

}

void hallWidget::on_pushButton_3_clicked()
{
    this->close();
}
